<?php

declare(strict_types=1);

namespace App\Job;

use App\Model\Device;
use App\Services\PushService;
use Hyperf\AsyncQueue\Job;

class PushDevice extends Job
{
    public $deviceId;

    /**
     * 任务执行失败后的重试次数，即最大执行次数为 $maxAttempts+1 次
     */
    protected int $maxAttempts = 2;


    public function __construct($deviceId)
    {
        // 这里最好是普通数据，不要使用携带 IO 的对象，比如 PDO 对象
        $this->deviceId = $deviceId;
    }

    public function handle()
    {
        $push = new PushService();
        $device = Device::find($this->deviceId);
        $push->sendDevice($device);
        if($device->port_status){
            $ports = json_decode($device->port_status,true);
            foreach($ports as $k => $v){
                $data['pileCode'] = $this->deviceId;
                $data['portName'] = $v['number'];
                $data['portNo'] = $v['number'];
                switch($v['status']){
                    case 1:
                        $status = 2;
                        break;
                    case 2:
                        $status = 1;
                        break;
                    case 3:
                        $status = 3;
                        break;
                    default:
                        $status = 4;

                }
                $data['portStatus'] = $status;
                $data['siteCode'] = $device->station_id;
                $result =  $push->sendPort($data);

            }
        }

    }
}
